package edu.pxxy.yzj;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.List;

import edu.pxxy.yzj.adapt.MenuAdapt;
import edu.pxxy.yzj.adapt.MenuAdaptEdit;
import edu.pxxy.yzj.dao.FoodDao;
import edu.pxxy.yzj.dao.FoodDaoImpl;
import edu.pxxy.yzj.domain.Food;

public class MenuManageActivity extends Activity {
    private Button addFood,findFood,delete,update;
    private EditText foodName,foodpri,foodCou,nameQuery;
    private FoodDao foodDao;
    private ListView foodQueryList;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage);
        foodDao= FoodDaoImpl.getInstence(this.getApplicationContext());
        initView();
        bindEvent();
    }

    private void bindEvent() {
        addFood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String foodname=foodName.getText().toString();
                Double foodprice=Double.parseDouble(foodpri.getText().toString());
                Integer foodcount=Integer.parseInt(foodCou.getText().toString());
                foodDao.addFood(new Food(foodname,foodprice,foodcount));
                Toast.makeText(getApplicationContext(), "添加菜品成功！", Toast.LENGTH_SHORT).show();
            }
        });
        findFood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String queryName=nameQuery.getText().toString();
                if (queryName.equals("")){
                    queryName=null;
                }
                Food food=new Food();
                food.setFoodname(queryName);
                List<Food> foodList=foodDao.queryFoodByFood(food);
                System.out.println(foodList);
                MenuAdaptEdit menuAdapt=new MenuAdaptEdit(MenuManageActivity.this,R.layout.edititem,foodList);
                foodQueryList.setAdapter(menuAdapt);
                foodQueryList.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initView() {
        addFood=findViewById(R.id.addFood);
        findFood=findViewById(R.id.findFood);
        foodName=findViewById(R.id.foodNa);
        foodpri=findViewById(R.id.foodpri);
        foodCou=findViewById(R.id.foodCou);
        nameQuery=findViewById(R.id.nameQuery);
        foodQueryList=findViewById(R.id.foodQueryList);
        delete=findViewById(R.id.delete);
        update=findViewById(R.id.update);
    }
}
